package com.cloudlibrary.mapper;

import com.github.pagehelper.Page;
import com.cloudlibrary.domain.Record;
import org.apache.ibatis.annotations.*;

@Mapper
public interface RecordMapper {
    @Insert({
            "<script>",
            "INSERT INTO record (",
            "<trim prefix='' suffix='' suffixOverrides=','>",
            "<if test='id != null'>record_id,</if>",
            "<if test='bookname != null'>record_bookname,</if>",
            "<if test='bookisbn != null'>record_bookisbn,</if>",
            "<if test='borrower != null'>record_borrower,</if>",
            "<if test='borrowTime != null'>record_borrowTime,</if>",
            "<if test='remandTime != null'>record_remandTime,</if>",
            "</trim>)",
            "VALUES (",
            "<trim prefix='' suffix='' suffixOverrides=','>",
            "<if test='id != null'>#{id},</if>",
            "<if test='bookname != null'>#{bookname},</if>",
            "<if test='bookisbn != null'>#{bookisbn},</if>",
            "<if test='borrower != null'>#{borrower},</if>",
            "<if test='borrowTime != null'>#{borrowTime},</if>",
            "<if test='remandTime != null'>#{remandTime},</if>",
            "</trim>)",
            "</script>"
    })
    @Results(id = "recordMap1", value = {
            //id字段默认为false，表示不是主键
            //column表示数据库表字段，property表示实体类属性名。
            @Result(id = true, column = "record_id", property = "id"),
            @Result(column = "record_bookname", property = "bookname"),
            @Result(column = "record_bookisbn", property = "bookisbn"),
            @Result(column = "record_borrower", property = "borrower"),
            @Result(column = "record_borrowtime", property = "borrowTime"),
            @Result(column = "record_remandtime", property = "remandTime")
    })
    Integer addRecord(Record record);

    @Select({"<script>" +
            "SELECT * FROM record " +
            "where 1=1" +
            "<if test=\"borrower != null\">AND record_borrower like  CONCAT('%',#{borrower},'%')</if>" +
            "<if test=\"bookname != null\">AND record_bookname  like  CONCAT('%',#{bookname},'%') </if>" +
            "order by record_remandtime DESC" +
            "</script>"
    })
    @Results(id = "recordMap", value = {
            //id字段默认为false，表示不是主键
            //column表示数据库表字段，property表示实体类属性名。
            @Result(id = true, column = "record_id", property = "id"),
            @Result(column = "record_bookname", property = "bookname"),
            @Result(column = "record_bookisbn", property = "bookisbn"),
            @Result(column = "record_borrower", property = "borrower"),
            @Result(column = "record_borrowtime", property = "borrowTime"),
            @Result(column = "record_remandtime", property = "remandTime")
    })
//查询借阅记录
    Page<Record> searchRecords(Record record);
}
